$(function(){
    Ext.onReady(function() {
        var obj = {
            formPanel: null,
            account: null,
            timer: null,
            djs: 0,
            init: function () {
                var that = this;
                that.account = localStorage.getItem('account') || '';
                if(!that.account){
                    Ext.MessageBox.alert('温馨提示', '账户信息过期，请重新登录！');
                    window.location.replace('./mobileLogin.html');
                    return;
                }
                that.formPanel = Ext.create('Ext.form.Panel', {
                    renderTo: document.body,
                    xtype: 'form',
                    title: '修改密码',
                    width: '100%',
                    layoutConfig:{columns:3},
                    bodyPadding: 10,
                    defaultType: 'textfield',
                    items: [{
                        fieldLabel: '账号',
                        name: 'account',
                        allowBlank : true,
                        readOnly : true,
                        value: that.account
                    }, {
                        inputType:'password',
                        fieldLabel: '新密码',
                        name: 'password',
                        tooltip: '请输入密码',
                        allowBlank: false,
                        blankText: '请输入密码',
                        regex: /^[\s\S]{0,32}$/,
                        regexText: '密码长度不能超过32个字符'
                    }, {
                        inputType:'password',
                        fieldLabel: '确认密码',
                        name: 'passwordAgain',
                        tooltip: '请输入密码',
                        allowBlank: false,
                        blankText: '请输入密码',
                        regex: /^[\s\S]{0,32}$/,
                        regexText: '密码长度不能超过32个字符'
                    }, {
                        inputType:'yzm',
                        fieldLabel: '短信验证码',
                        name: 'yzm',
                        tooltip: '请输入验证码',
                        allowBlank: false,
                        blankText: '请输入验证码'
                    }, {
                        id: 'getYZMBar',
                        xtype: 'button',
                        text: "发送验证码",
                        style: {
                            position: 'absolute',
                            left: '300px',
                            'margin-top': '-5px',
                            transform: 'translate(0,-100%);'
                        },
                        listeners: {
                            render: function (obj) {
                                var font=document.createElement("font");
                                font.setAttribute("id","djs");
                                font.setAttribute("color","red");
                                font.setAttribute("style","position:absolute;left: 400px;margin-top: -25px;");
                                var tips=document.createTextNode('');
                                font.appendChild(tips);

                                obj.el.dom.parentNode.appendChild(font);
                            },
                        },
                        handler: function() {
                            if(that.djs!=0){
                                $.alert('验证码发送频繁，请在'+that.djs+'s后重试',2800,'center');
                                return
                            }
                            that.startDJS(this);
                            that.ajaxGetYZM();
                        }
                    }
                    ],
                    buttons: [
                        {
                            text: '保存',
                            handler: function() {
                                var form = this.up('form'); // get the form panel
                                if (form.isValid()) {
                                    that.formPanel.form.doAction('submit', {
                                        url :'22',//$.getAjaxHost('def') + 'zpinfo/resetPassword',
                                        method : 'POST',
                                        waitTitle : '连接中',
                                        waitMsg : '传送数据...',
                                        success : function() {
                                            Ext.Msg.alert('提示',
                                                '修改成功!'
                                            );
                                            that.formPanel.getForm().reset();
                                        },
                                        failure : function(form, action) {
                                            Ext.Msg.alert('提示', '非常抱歉!,修改失败');
                                        }
                                    });

                                }
                            }
                        }
                    ]
                });

            },
            // 获取验证码
            ajaxGetYZM: function() {
                var that = this;
                $.getData({
                    url: $.getAjaxHost('def') + 'zpinfo/getYZM',
                    //url: $.getAjaxHost('def') + 'cxjy/zpinfo/getList',
                    type: 'get',
                    contentType: 'application/json',
                    data: {
                        tel:encodeURI(that.account),
                    },
                    success: function (res) {
                        $.alert('验证码发送成功',2800,'center')
                    },
                    error: function (res) {
                        that.djs = 0;
                        $.alert('验证码发送失败',2800,'center')
                    }
                })
            },
            // 倒计时
            startDJS:function (obj) {
                var that = this;
                clearInterval(that.timer);
                that.djs = 30;
                $('#djs').html(that.djs+'s后重试');
                that.timer = setInterval(function () {
                    if(that.djs>0){
                        $('#djs').html(that.djs+'s后重试');
                        that.djs --;
                    }else{
                        $('#djs').html('');
                        clearInterval(that.timer);
                    }
                },1000);
            }
        };
        obj.init();
    });
});